/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package jktest;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;

/**
 *
 * Class integrating a MouseListener to simualte an HTML Hyperlink
 *
 * @author Royce Feng
 */
public class JKHyperLabel extends JLabel implements MouseListener {

    private String link;

    /**
     *
     * Creates a new JLabel with the text underlined and blue, sets the cursor
     * to a Hand cursor, and adds the Mouse Listener
     *
     * @param text The text to be displayed
     */
    public JKHyperLabel ( String text ) {
        super("<html><u>"+text+"</u></html>");
        this.setForeground( Color.BLUE );
        link = text;
        Cursor newCursor = new Cursor(Cursor.HAND_CURSOR);
        setCursor(newCursor);
        this.addMouseListener(this);
    }

    /**
     *
     * Mouse event that actually matters. Runs the selected URL via the
     * preferred browser of the operating system
     *
     * @param e The Mouse Event
     */
    public void mouseClicked(MouseEvent e) {
        String osName = System.getProperty("os.name");
        if (osName.toUpperCase().startsWith(("WINDOWS"))) {
            try {
                Runtime.getRuntime().exec("cmd /c start " + link);
            } catch (IOException ex) {
            }
        } else {
            JOptionPane.showMessageDialog(this, "Current Operating System is not windows", "Unrecognized Operating System", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void mouseExited( MouseEvent e ) {}
    public void mouseEntered( MouseEvent e ) {}
    public void mouseReleased ( MouseEvent e ) {}
    public void mousePressed ( MouseEvent e ) {}

}
